home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Magazine / YAM / Source / starter.c < prev    next >
C/C++ Source or Header  |  2000-11-18  |  2KB  |  122 lines

  1. /*************************************************************************
  2.  
  3.   starter.c
  4.  
  5.   Copyright 2000 Emmanuel Lesueur
  6.  
  7.   Automatically select between an AmigaOS 68k executable and a native
  8.   MorphOS ppc one.
  9.  
  10.   Usage:
  11.  
  12.   To make two version of the program 'PROG', compile this program with
  13.   a 68k gcc with:
  14.  
  15.      gcc -nostdlib -o PROG -O2 -noixemul -DNAME="\"PROG\"" starter.c
  16.  
  17.   Then name the 68k version of 'PROG' 'PROG.amigaos', the MorphOS version
  18.   'PROG.morphos'.
  19.  
  20.   This code is in the public domain. Use it as you like.
  21.  
  22. *************************************************************************/
  23.  
  24. #define __NOLIBBASE
  25. #include <exec/exec.h>
  26. #include <dos/dos.h>
  27. #include <dos/dosextens.h>
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <proto/intuition.h>
  31.  
  32. #define REG(r, x) x __asm(#r)
  33.  
  34. asm("
  35.     .text
  36.     bra _start
  37. ");
  38.  
  39. int call(REG(a0, char *line), REG(d0, int len), REG(a1, void *start));
  40. asm("
  41.     .text
  42. _call:
  43.     moveml    d2-d7/a2-a6,sp@-
  44.     jbsr     (a1)
  45.     moveml    sp@+,d2-d7/a2-a6
  46.     rts
  47. ");
  48.  
  49.  
  50. int start(REG(a0, char *line), REG(d0, int len)) {
  51.     struct Library *SysBase = *(struct Library **)4;
  52.     struct Library *DOSBase;
  53.     struct Library *IntuitionBase;
  54.     struct Process *proc = (struct Process *)FindTask(NULL);
  55.     struct Message *wbmsg;
  56.     int ret = RETURN_FAIL;
  57.     BPTR seg = 0;
  58.  
  59.     if (proc->pr_CLI)
  60.     wbmsg = NULL;
  61.     else {
  62.     WaitPort(&proc->pr_MsgPort);
  63.     wbmsg = GetMsg(&proc->pr_MsgPort);
  64.     }
  65.  
  66.     if (DOSBase = OpenLibrary("dos.library", 39)) {
  67.  
  68.     if (FindResident("MorphOS")) {
  69.         seg = LoadSeg("PROGDIR:" NAME ".morphos");
  70.     }
  71.  
  72.     if (!seg)
  73.         seg = LoadSeg("PROGDIR:" NAME ".amigaos");
  74.  
  75.     if (seg) {
  76.         int (*start)(REG(a0, char *line), REG(d0, int len));
  77.  
  78.         start = (void *)((BPTR *)BADDR(seg) + 1);
  79.  
  80.         if (wbmsg) {
  81.         PutMsg(&proc->pr_MsgPort, wbmsg);
  82.         wbmsg = NULL;
  83.         }
  84.  
  85.         ret = call(line, len, start);
  86.  
  87.         UnLoadSeg(seg);
  88.     }
  89.  
  90.     if (!seg) {
  91.         if (wbmsg) {
  92.         
  93.         if (IntuitionBase = (APTR)OpenLibrary("intuition.library", 39)) {
  94.             static struct EasyStruct params = {
  95.             sizeof(struct EasyStruct),
  96.             0,
  97.             "Error",
  98.             "Can't load \"PROGDIR:" NAME ".amigaos\".",
  99.             "Ok"
  100.             };
  101.  
  102.             EasyRequestArgs(NULL, ¶ms, NULL, NULL);
  103.  
  104.             CloseLibrary(IntuitionBase);
  105.         }
  106.  
  107.         } else
  108.         PutStr("Can't load \"PROGDIR:" NAME ".amigaos\".\n");
  109.     }
  110.  
  111.     CloseLibrary(DOSBase);
  112.     }
  113.  
  114.     if (wbmsg) {
  115.     Forbid();
  116.     ReplyMsg(wbmsg);
  117.     }
  118.  
  119.     return ret;
  120. }
  121.  
  122.